package main

import (
	"fmt"
	"net/http"
)

func index(w http.ResponseWriter, r *http.Request) {
	_, err := w.Write([]byte("hello world"))
	if err != nil {
		fmt.Println("err:", err)
	}
}

func httpServerByFunc() {
	// http.HandleFunc("/", index)
	http.Handle("/", http.HandlerFunc(index))
	http.HandleFunc("/end", func(w http.ResponseWriter, r *http.Request) {
		panic("close...")
	})
	err := http.ListenAndServe(":5000", nil)
	if err != nil {
		panic(err)
	}
}

type MyHandler struct {
}

func (my *MyHandler) ServeHTTP(w http.ResponseWriter, r http.Request) {
	_, err := w.Write([]byte("hello world"))
	if err != nil {
		fmt.Println("err:", err)
	}
}
func httpServerByStruct() {
	mux := http.NewServeMux()
	myHandler := MyHandler{}
	mux.Handle("/", &myHandler)
	err := http.ListenAndServe(":5000", mux)
	if err != nil {
		panic(err)
	}
}

func main() {
	httpServerByFunc()
}
